package ru.ifmo.filemanager.filesystem.requests;

import static ru.ifmo.filemanager.core.Core.error;

import java.io.IOException;

import ru.ifmo.filemanager.filesystem.Type;
import ru.ifmo.filemanager.filesystem.interfaces.Directory;
import ru.ifmo.filemanager.filesystem.interfaces.DirectoryLookupReceiver;
import ru.ifmo.filemanager.filesystem.interfaces.FileSystemObject;

public class ActivateRequest extends ChangeDirectoryRequest implements DirectoryLookupReceiver {

	public ActivateRequest activate(Directory directory, String name) {
		directory.lookupAsync(name, this);
		return this;
	}

	public void directoryLookupSuccess(Directory self, String name, FileSystemObject result) {
		if (cancelled) {
			return;
		}
		if (result == null || result.type() != Type.DIRECTORY) {
			cancel();
			return;
		}
		changeDirectory((Directory) result, 1);
	}

	public void directoryLookupFailure(Directory self, String name,
		IOException error) {
		if (cancelled) {
			return;
		}
		error("Cannot look up object", error, false);
		cancel();
	}
}
